//## begin module%3B882542012C.cm preserve=no
//	  %X% %Q% %Z% %W%
//## end module%3B882542012C.cm

//## begin module%3B882542012C.cp preserve=no
//	+----------------------------------------------+
//	| SVMBR - A program for training SVMs          |
//	+----------------------------------------------+
//	| * Creator, modeling, coding:                 |
//	|    Marcelo Barros de Almeida                 |
//	|    barros@smar.com.br                        |
//	|    http://litc.cpdee.ufmg.br/~barros/        |
//	| * Coding, improvements, bug fixes:           |
//	|    Bernardo Penna                            |
//	|    bprc@brfree.com.br                        |
//	+----------------------------------------------+
//
//	 Copyright(c) 2002 by Marcelo Barros de Almeida
//	                            All rights reserved
//
//	-[CVS]------------------------------------------
//	$Author$
//	$Date$
//	$Source$
//	$Name$
//	$Revision$
//	------------------------------------------------
//## end module%3B882542012C.cp

//## Module: data%3B882542012C; Package body
//## Subsystem: <Top Level>
//## Source file: D:\textotese\svmbr\src\data.cpp

//## begin module%3B882542012C.additionalIncludes preserve=no
//## end module%3B882542012C.additionalIncludes

//## begin module%3B882542012C.includes preserve=yes
//## end module%3B882542012C.includes

// data
#include "data.h"
//## begin module%3B882542012C.declarations preserve=no
//## end module%3B882542012C.declarations

//## begin module%3B882542012C.additionalDeclarations preserve=yes
//## end module%3B882542012C.additionalDeclarations


// Class Data 






//## begin Data::NORMAL%3B86C46D02E0.attr preserve=no  public: static SHORT {U} 1
SHORT Data::NORMAL = 1;
//## end Data::NORMAL%3B86C46D02E0.attr

//## begin Data::SPARSE%3B86C4F0016C.attr preserve=no  public: static SHORT {U} 2
SHORT Data::SPARSE = 2;
//## end Data::SPARSE%3B86C4F0016C.attr

//## begin Data::BINARY%3B86C4FE0324.attr preserve=no  public: static SHORT {U} 3
SHORT Data::BINARY = 3;
//## end Data::BINARY%3B86C4FE0324.attr











//## Other Operations (implementation)
VOID Data::setfileName (CHAR *file)
{
  //## begin Data::setfileName%998880772.body preserve=yes

  	strncpy(fileName,file,_MAX_PATH-1);

  //## end Data::setfileName%998880772.body
}

VOID Data::setTarget (INT n)
{
  //## begin Data::setTarget%1002289385.body preserve=yes
  INT i;

	numClassA = 0;
	numClassB = 0;

	// allocate ntarget if it was not done yet
	if(!multiClass){
		multiClass = TRUE;
    try {
      ntarget = new INT [numExamples];
		}
    catch(...){
      cerr << "Error when allocating memory (Data::setTarget)." << endl;
		  exit(1);
    }
		// copy and set new target
		for(i = 0 ; i < numExamples ; i++){
			ntarget[i] = target[i];
			if (ntarget[i] == n){
				target[i] = 1;
				numClassA++;
			}
			else {
	        target[i] = -1;
			    numClassB++;
			}
		}
		return; //avoid two loops
	}


  for(i = 0 ; i < numExamples ; i++){
    if (ntarget[i] == n){
      target[i] = 1;
      numClassA++;
    }
    else {
      target[i] = -1;
      numClassB++;
    }
  }
  //## end Data::setTarget%1002289385.body
}

// Additional Declarations
  //## begin Data%3B86C348005E.declarations preserve=yes
  //## end Data%3B86C348005E.declarations

// Class SparseData 



SparseData::SparseData (CHAR *file)
  //## begin SparseData::SparseData%998688398.hasinit preserve=no
  //## end SparseData::SparseData%998688398.hasinit
  //## begin SparseData::SparseData%998688398.initialization preserve=yes
  //## end SparseData::SparseData%998688398.initialization
{
  //## begin SparseData::SparseData%998688398.body preserve=yes

	dataType = Data::SPARSE;
	setfileName(file);
	loadData();
	try {
		fullVec = new REAL [dim];
	}
  catch(...){
		cerr << "Error when allocating memory (SparseData::SparseData)." << endl;
		exit(1);
  }
	multiClass = FALSE;

  //## end SparseData::SparseData%998688398.body
}


SparseData::~SparseData()
{
  //## begin SparseData::~SparseData%.body preserve=yes

		INT i;

		for(i = 0 ; i < numExamples ; i++){
			delete point[i];
			delete index[i];
		}
		
		delete index;
		delete point;
		delete target;
		delete fullVec;
		delete indexSize;
		if(multiClass) delete ntarget;

  //## end SparseData::~SparseData%.body
}



//## Other Operations (implementation)
REAL SparseData::dotProd (UINT i1, UINT i2)
{
  //## begin SparseData::dotProd%998688345.body preserve=yes

  INT x1 = 0;
  INT x2 = 0;
  REAL tmp = 0;
  REAL z1 = 0;
  REAL z2 = 0;
  INT num1 = indexSize[i1];
  INT num2 = indexSize[i2];

  while ((x1 < num1) && (x2 < num2)) {
    z1 = index[i1][x1];
    z2 = index[i2][x2];
    if (z1 == z2) {
      tmp += point[i1][x1]*point[i2][x2];
      x1++;
      x2++;
    }
    else {
      if (z1 > z2)
        x2++;
      else
        x1++;
    }
  }

  return tmp;

  //## end SparseData::dotProd%998688345.body
}

INT SparseData::loadData (CHAR *file)
{
  //## begin SparseData::loadData%998880769.body preserve=yes

	setfileName(file);
	return loadData();

  //## end SparseData::loadData%998880769.body
}

INT SparseData::loadData ()
{
  //## begin SparseData::loadData%998880773.body preserve=yes

	INT i, j, k, vecSize;
  REAL tmp;
  REAL *dataRow; 

  ifstream file(fileName);

  if (! file.is_open()) {
    cerr << "Error when opening (SparseData::loadData)." << fileName << endl;
    exit(1);
  }

	cout << "Loading data." << endl << flush;
  file >> numExamples;
  file >> dim;

	try{
    target		= new INT [numExamples];
    indexSize = new INT [numExamples];
		dataRow		= new REAL [dim];  
    point			= new REAL *[numExamples];
		index			= new INT  *[numExamples];
  }
  catch(...){
    cerr << "Error when allocating memory (SparseData::loadData)." << endl;
		exit(1);
  }

	numClassA = 0;
	numClassB = 0;

  for (i = 0; i < numExamples; i++) {
		for (j = 0; j < dim; j++)
      file >> dataRow[j];

    for (j = 0, vecSize = 0; j < dim; j++)
      if (dataRow[j])
        vecSize++;

    try {
      point[i] = new REAL[vecSize];
		  index[i] = new INT[vecSize];
		}
    catch(...){
      cerr << "Error when allocating memory (SparseData::loadData)." << endl;
		  exit(1);
    }

    indexSize[i] = vecSize;

    for (j = 0, k = 0; j < dim; j++) {
			if (dataRow[j]) {
				index[i][k] = j;
				point[i][k] = dataRow[j];
        k++;
			}
		}

    file >> tmp;
    target[i] = (INT) tmp;

    /* always will existe a class 1 but we can not say the same about 
       class -1 if multiclass is used. In this case, setTarget will do
       the job.
    */
    if(target[i] == 1)
	    numClassA++;
    else
	    numClassB++;
  }

  file.close();
  delete dataRow;
  return TRUE;

  //## end SparseData::loadData%998880773.body
}

REAL * SparseData::getpoint (INT i1)
{
  //## begin SparseData::getpoint%998880789.body preserve=yes
  INT j, k;

  for (j = 0, k = 0; j < dim; j++) {
	  if (index[i1][k] == j) {
		  fullVec[j] = point[i1][k];
		 	k++;
		}
    else
      fullVec[j] = 0;
  }

	return fullVec; // only for non-concurrent programming !!!
  //## end SparseData::getpoint%998880789.body
}

REAL SparseData::dotProd (REAL *vec, UINT i2)
{
  //## begin SparseData::dotProd%999139416.body preserve=yes

  INT j, k;
  REAL tmp = 0;

  for (j = 0, k = 0; j < dim; j++) {
	  if (index[i2][k] == j) {
			tmp += vec[j]*point[i2][k];
		 	k++;
		}
  }

		return tmp;

  //## end SparseData::dotProd%999139416.body
}

REAL SparseData::dotProd (REAL *vec1, REAL *vec2)
{
  //## begin SparseData::dotProd%999139417.body preserve=yes

	INT i;
	REAL tmp = 0;
	for(i = 0 ; i < dim ; i++)
		tmp += vec1[i]*vec2[i];

	return tmp;

  //## end SparseData::dotProd%999139417.body
}

// Additional Declarations
  //## begin SparseData%3B86C7E90383.declarations preserve=yes
  //## end SparseData%3B86C7E90383.declarations

// Class NormalData 

NormalData::NormalData (CHAR *file)
  //## begin NormalData::NormalData%998688399.hasinit preserve=no
  //## end NormalData::NormalData%998688399.hasinit
  //## begin NormalData::NormalData%998688399.initialization preserve=yes
  //## end NormalData::NormalData%998688399.initialization
{
  //## begin NormalData::NormalData%998688399.body preserve=yes

	dataType = Data::NORMAL;
  setfileName(file);
	loadData();
	multiClass = FALSE;
  //## end NormalData::NormalData%998688399.body
}


NormalData::~NormalData()
{
  //## begin NormalData::~NormalData%.body preserve=yes

		INT i;

		for(i = 0 ; i < numExamples ; i++)
			delete point[i];
		
		delete point;
		delete target;
		if(multiClass) delete ntarget;

  //## end NormalData::~NormalData%.body
}



//## Other Operations (implementation)
REAL NormalData::dotProd (UINT i1, UINT i2)
{
  //## begin NormalData::dotProd%998688346.body preserve=yes

	INT i;
	REAL tmp = 0;
	for(i = 0 ; i < dim ; i++)
		tmp += point[i1][i]*point[i2][i];

	return tmp;

  //## end NormalData::dotProd%998688346.body
}

INT NormalData::loadData (CHAR *file)
{
  //## begin NormalData::loadData%998880770.body preserve=yes

	setfileName(file);
	return loadData();

  //## end NormalData::loadData%998880770.body
}

INT NormalData::loadData ()
{
  //## begin NormalData::loadData%998880774.body preserve=yes

	INT i, j;
    ifstream file(fileName);
	REAL tmp;

  if (file.is_open() == 0 ) {
    cerr << "Error when opening " << fileName << endl;
    exit(1);
  }

	cout << "Loading data." << endl << flush;
  file >> numExamples;
  file >> dim;

	try{
		point = new REAL *[numExamples];
		for(i = 0 ; i < numExamples ; i++)
			point[i] = new REAL[dim];

		target = new INT [numExamples];
	} 
	catch(...){
		cerr << "Error when allocating memory (NormalData::loadData)." << endl;
		return FALSE;
	}

	numClassA = 0;
	numClassB = 0;

  for (i = 0; i < numExamples; i++) {
    for (j = 0; j < dim; j++)
      file >> point[i][j];
		file >> tmp;
		target[i] = (INT) tmp;

		if(target[i] == 1)
			numClassA++;
		else
			numClassB++;
  }

  file.close();
  return TRUE;

  //## end NormalData::loadData%998880774.body
}

REAL * NormalData::getpoint (INT i1)
{
  //## begin NormalData::getpoint%998880790.body preserve=yes

	return point[i1];

  //## end NormalData::getpoint%998880790.body
}

REAL NormalData::dotProd (REAL *vec, UINT i2)
{
  //## begin NormalData::dotProd%999139418.body preserve=yes

	INT i;
	REAL tmp = 0;
	for(i = 0 ; i < dim ; i++)
		tmp += vec[i]*point[i2][i];

	return tmp;

  //## end NormalData::dotProd%999139418.body
}

REAL NormalData::dotProd (REAL *vec1, REAL *vec2)
{
  //## begin NormalData::dotProd%999139419.body preserve=yes

	INT i;
	REAL tmp = 0;
	for(i = 0 ; i < dim ; i++)
		tmp += vec1[i]*vec2[i];

	return tmp;

  //## end NormalData::dotProd%999139419.body
}

// Additional Declarations
  //## begin NormalData%3B86D86103A9.declarations preserve=yes
  //## end NormalData%3B86D86103A9.declarations

// Class BinaryData 



BinaryData::BinaryData (CHAR *file)
  //## begin BinaryData::BinaryData%998688400.hasinit preserve=no
  //## end BinaryData::BinaryData%998688400.hasinit
  //## begin BinaryData::BinaryData%998688400.initialization preserve=yes
  //## end BinaryData::BinaryData%998688400.initialization
{
  //## begin BinaryData::BinaryData%998688400.body preserve=yes

	dataType = Data::BINARY;
	setfileName(file);
	loadData();
	try {
		fullVec = new REAL [dim];
	}
  catch(...){
		cerr << "Error when allocating memory (BinaryData::BinaryData)." << endl;
		exit(1);
  }
	multiClass = FALSE;
  //## end BinaryData::BinaryData%998688400.body
}


BinaryData::~BinaryData()
{
  //## begin BinaryData::~BinaryData%.body preserve=yes

		INT i;

		for(i = 0 ; i < numExamples ; i++){
			delete index[i];
		}
		
		delete index;
		delete target;
		delete fullVec;
		delete indexSize;
		if(multiClass) delete ntarget;

  //## end BinaryData::~BinaryData%.body
}



//## Other Operations (implementation)
REAL BinaryData::dotProd (UINT i1, UINT i2)
{
  //## begin BinaryData::dotProd%998688347.body preserve=yes

	INT x1 = 0;
  INT x2 = 0;
  REAL tmp = 0;
  REAL z1 = 0;
  REAL z2 = 0;
  INT num1 = indexSize[i1];
  INT num2 = indexSize[i2];

  while ((x1 < num1) && (x2 < num2)) {
    z1 = index[i1][x1];
    z2 = index[i2][x2];
    if (z1 == z2) {
      tmp ++;
      x1++;
      x2++;
    }
    else {
      if (z1 > z2)
        x2++;
      else
        x1++;
    }
  }

  return tmp;

  //## end BinaryData::dotProd%998688347.body
}

INT BinaryData::loadData (CHAR *file)
{
  //## begin BinaryData::loadData%998880771.body preserve=yes

	setfileName(file);
	return loadData();

  //## end BinaryData::loadData%998880771.body
}

INT BinaryData::loadData ()
{
  //## begin BinaryData::loadData%998880775.body preserve=yes

	INT i, j, k, vecSize;
  REAL tmp;
  REAL *dataRow; 

  ifstream file(fileName);

  if (! file.is_open()) {
    cerr << "Error when opening (BinaryData::loadData)." << fileName << endl;
    exit(1);
  }

	cout << "Loading data." << endl << flush;
  file >> numExamples;
  file >> dim;

	try{
    target		= new INT [numExamples];
    indexSize = new INT [numExamples];
		dataRow		= new REAL [dim];  
    index			= new INT  *[numExamples];
  }
  catch(...){
    cerr << "Error when allocating memory (BinaryData::loadData)." << endl;
		exit(1);
  }

	numClassA = 0;
	numClassB = 0;

  for (i = 0; i < numExamples; i++) {
		for (j = 0; j < dim; j++)
      file >> dataRow[j];

    for (j = 0, vecSize = 0; j < dim; j++)
      if (dataRow[j])
        vecSize++;

    try {
      index[i] = new INT[vecSize];
		}
    catch(...){
      cerr << "Error when allocating memory (BinaryData::loadData)." << endl;
		  exit(1);
    }

    indexSize[i] = vecSize;

    for (j = 0, k = 0; j < dim; j++) {
			if (dataRow[j]) {
				index[i][k] = j;
			  k++;
			}
		}

    file >> tmp;
    target[i] = (INT) tmp;

    if(target[i] == 1)
      numClassA++;
    else
      numClassB++;

  }

  file.close();
  delete dataRow;
  return TRUE;

  //## end BinaryData::loadData%998880775.body
}

REAL * BinaryData::getpoint (INT i1)
{
  //## begin BinaryData::getpoint%998880791.body preserve=yes
	  INT j, k;

  for (j = 0, k = 0; j < dim; j++) {
	  if (index[i1][k] == j) {
		  fullVec[j] = 1;
		 	k++;
		}
    else
      fullVec[j] = 0;
  }

	return fullVec; // only for non-concurrent programming !!!
  //## end BinaryData::getpoint%998880791.body
}

REAL BinaryData::dotProd (REAL *vec, UINT i2)
{
  //## begin BinaryData::dotProd%999139420.body preserve=yes
  INT j, k;
  REAL tmp = 0;

  for (j = 0, k = 0; j < dim; j++) {
	  if (index[i2][k] == j) {
		  tmp += vec[j];
		 	k++;
		}
  }

	return tmp;
  //## end BinaryData::dotProd%999139420.body
}

REAL BinaryData::dotProd (REAL *vec1, REAL *vec2)
{
  //## begin BinaryData::dotProd%999139421.body preserve=yes
	INT i;
	REAL tmp = 0;
	for(i = 0 ; i < dim ; i++)
		tmp += vec1[i]*vec2[i];

	return tmp;
  //## end BinaryData::dotProd%999139421.body
}

// Additional Declarations
  //## begin BinaryData%3B86D93D01A6.declarations preserve=yes
  //## end BinaryData%3B86D93D01A6.declarations

//## begin module%3B882542012C.epilog preserve=yes
//## end module%3B882542012C.epilog
